﻿Imports DTO
Imports System.Data.OleDb

Public Class PhongDAO
    'LOAD DANH SÁCH PHÒNG
    Public Function HienThiDanhSachPhong() As List(Of PhongDTO)
        Dim con As OleDbConnection = DataProvider.ConnectionData()
        Dim strSQL As String = "select p.MaPhong, p.TenPhong, lp.TenLoaiPhong, tt.TenTinhTrang, p.GhiChu,lp.DonGia from PHONG p, LOAIPHONG lp, TINHTRANG tt"
        strSQL += " where p.MaLoaiPhong=lp.MaLoaiPhong and tt.MaTinhTrang=p.MaTinhTrang"

        Dim command As New OleDbCommand()
        command.Connection = con
        command.CommandType = CommandType.Text
        command.CommandText = strSQL

        Dim dsPhong As New List(Of PhongDTO)
        Dim phong As PhongDTO
        Dim reader As OleDbDataReader = command.ExecuteReader()

        While (reader.Read())

            phong = New PhongDTO()
            phong.MaPhong = reader.GetInt32(0)
            phong.TenPhong = reader.GetString(1)
            phong.TenLoaiPhong = reader.GetString(2)
            phong.TenTinhTrang = reader.GetString(3)
            phong.GhiChu = reader.GetString(4)
            phong.GiaPhong = reader.GetDouble(5)
            dsPhong.Add(phong)

        End While
        con.Close()
        Return dsPhong
    End Function

    'INSERT VÀO BẢNG PHÒNG
    Public Function InsertPhong(ByVal phong As PhongDTO) As Integer
        Dim con As OleDbConnection = DataProvider.ConnectionData()
        Dim strSQL As String = "insert into PHONG(TenPhong, MaLoaiPhong, GhiChu, MaTinhTrang)"
        strSQL += " values(@tenphong, @maloaiphong, @ghichu, @matinhtrang)"

        Dim command As New OleDbCommand()
        command.Connection = con
        command.CommandType = CommandType.Text
        command.CommandText = strSQL

        Dim para As OleDbParameter
        command.Parameters.Add("@tenphong", OleDbType.VarChar)
        para.Value = phong.TenPhong

        command.Parameters.Add("@maloaiphong", OleDbType.Integer)
        para.Value = phong.MaLoaiPhong

        command.Parameters.Add("@ghichu", OleDbType.VarChar)
        para.Value = phong.GhiChu

        command.Parameters.Add("@matinhtrang", OleDbType.Integer)
        para.Value = phong.MaTinhTrang


        If command.ExecuteNonQuery() <> 0 Then
            Return 1 'insert thành công
        Else
            Return 0 'insert thất bại
        End If


    End Function

End Class
